package com.jqr.activity.basicstatedemo


import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
@SuppressLint("UnrememberedMutableState")
@Composable
fun WaterCounter(modifier: Modifier=Modifier){
    /**
     * 运行应用并点击按钮时，您会发现没有任何反应。为 count 变量设置不同的值不会使 Compose 将其检测为状态更改，
     * 因此不会产生任何效果。这是因为，当状态发生变化时，您尚未指示 Compose 应重新绘制屏幕
     */
    //var  count=0 //count 不是状态改变变量，修改
//    val count:MutableState<Int> = remember {
//        mutableStateOf(0)
//    }



    Column {
//        Text(text = "你已经喝了 ${count.value} 杯水", modifier = Modifier.padding(16.dp))
//
//        Button(
//            onClick = {
//                count.value++
//                System.out.println(count)
//            },
//
//            Modifier.padding(top = 8.dp)) {
//            Text(text = "添加一杯")
//        }
        // rememberSaveable | remember
        //虽然 remember 可帮助您在重组后保持状态，
        // 但不会帮助您在配置更改后保持状态。为此，您必须使用 rememberSaveable，而不是 remember。
        var  count by rememberSaveable {
            mutableStateOf(0)
        }

        if(count>0){
            var showTask by remember {
                mutableStateOf(true)
            }
            if(showTask){
                WaterTaskItem(taskName = "是否需要进行30分钟不行", onClose = { showTask=false })
            }
            Text(text = "你已经喝了 ${count} 杯水", modifier = Modifier.padding(16.dp))
        }
        Row {
            Button(
                onClick = {
                    count++
                    System.out.println(count)
                },
                Modifier.padding(top = 8.dp),
                enabled = count<8

            ) {
                Text(text = "添加一杯")
            }
            Button(onClick = { count=0},Modifier.padding(top = 8.dp),) {
                Text(text = "重置")
            }
        }

    }


}
@Composable
fun  StatelessWaterCount(count:Int,onIncrement:()->Unit,modifier: Modifier=Modifier){

    Column(modifier=Modifier.padding(16.dp)) {
        if(count>0){
            Text(text = "你已经喝了 ${count} 杯水", modifier = Modifier.padding(16.dp))
        }
        
        Button(onClick = onIncrement,Modifier.padding(top = 8.dp), enabled = count<8) {

            Text(text = "添加一杯")
        }
    }



}